using System;
using System.Collections.Generic;

namespace Ants
{

    class MyBot : Bot
    {

        // DoTurn is run once per turn
        public override void DoTurn(IGameState state)
        {
            AntHelper.Moves.Clear();
          
            // loop through all my ants and try to give them orders
            foreach (Ant ant in state.MyAnts)
            {
                Direction bestDirection = ChooseDirection(state, ant);

                IssueOrder(ant, bestDirection);
                AntHelper.Moves.Add(state.GetDestination(ant, bestDirection));

                // check if we have time left to calculate more orders
                if (state.TimeRemaining < 10) break;
            }





        }
        protected Direction ChooseDirection(IGameState state, Ant ant)
        {
            List<Location> foodTiles = state.FoodTiles;

            int bestDistance = 1000000;

            if (foodTiles.Count > 0)
            {
                Location bestTile = foodTiles[0];
                foreach (Location foodTile in foodTiles)
                {

                    int distance = state.GetDistance(ant, foodTile);

                    if (distance <= bestDistance)
                    {
                        if (AntHelper.IsValidPath(state, ant, foodTile))
                        {
                            bestDistance = distance;
                            bestTile = foodTile;
                        }
                    }
                }
                return AntHelper.GetFirstDirection(state, ant, bestTile);
            }
            foreach (Direction d in Ants.Aim.Keys)
            {
                if (AntHelper.IsValidTile(state, state.GetDestination(ant, d)))
                {
                    return d;
                }
            }
            return Direction.South;
        }


        public static void Main(string[] args)
        {
            new Ants().PlayGame(new MyBot());
        }

    }

}